﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;

namespace YRevitJig
{
	public class XLineJig : XUiJigBase, IDisposable
	{
		public XLineJig(UIDocument uiDoc) : base(uiDoc)
		{
			this.JigStage = XLineJig.EStage.eNoOper;
			this.ResetPoint = XUiJigBase.EResetPoint.eNoOper;
            this.DragLineColor = System.Drawing.Color.FromArgb(175, 222, 239);
			this.PointAlignType = XLineJig.AlignType.eMiddle;
			this.MutilLineDist = new List<double>();
			this.DrawEndLine = false;
			this.DrawSize = false;
			int height = Screen.PrimaryScreen.Bounds.Height;
			XLineJig.SizeOffset = (int)(0.006 * (double)height);
			XLineJig.BorderLength = (int)(0.04 * (double)height);
			XLineJig.SizeFont = new Font("Arial", 2.5f * (float)XLineJig.SizeOffset, GraphicsUnit.Pixel);
			XLineJig.TextFormat = new StringFormat(StringFormatFlags.NoClip);
			XLineJig.TextFormat.Alignment = StringAlignment.Center;
			XLineJig.TextFormat.LineAlignment = StringAlignment.Center;
            XLineJig.SizeLineColor = System.Drawing.Color.FromArgb(126, 163, 255);
			XLineJig.SizeTextBrush = new SolidBrush(base.JigPen.Color);
			this.OnMouseLeaveFormClientEvent = (WndMsgManager.MouseLeaveFormClientEvnetHandler)Delegate.Combine(this.OnMouseLeaveFormClientEvent, new WndMsgManager.MouseLeaveFormClientEvnetHandler(this.OnMouseLeaveFormClientEvnetHandler));
		}

        public XLineJig(UIDocument uiDoc, System.Windows.Forms.Form userForm)
            : base(uiDoc)
		{
			this.JigStage = XLineJig.EStage.eNoOper;
			this.ResetPoint = XUiJigBase.EResetPoint.eNoOper;
            this.DragLineColor = System.Drawing.Color.FromArgb(175, 222, 239);
			this.PointAlignType = XLineJig.AlignType.eMiddle;
			this.MutilLineDist = new List<double>();
			this.DrawEndLine = false;
			this.DrawSize = false;
			int height = Screen.PrimaryScreen.Bounds.Height;
			XLineJig.SizeOffset = (int)(0.006 * (double)height);
			XLineJig.BorderLength = (int)(0.04 * (double)height);
			XLineJig.SizeFont = new Font("Arial", 2.5f * (float)XLineJig.SizeOffset, GraphicsUnit.Pixel);
			XLineJig.TextFormat = new StringFormat(StringFormatFlags.NoClip);
			XLineJig.TextFormat.Alignment = StringAlignment.Center;
			XLineJig.TextFormat.LineAlignment = StringAlignment.Center;
            XLineJig.SizeLineColor = System.Drawing.Color.FromArgb(126, 163, 255);
			XLineJig.SizeTextBrush = new SolidBrush(base.JigPen.Color);
			this.OnMouseLeaveFormClientEvent = (WndMsgManager.MouseLeaveFormClientEvnetHandler)Delegate.Combine(this.OnMouseLeaveFormClientEvent, new WndMsgManager.MouseLeaveFormClientEvnetHandler(this.OnMouseLeaveFormClientEvnetHandler));
			this.OnMouseMoveIntoFormClientEvent = (WndMsgManager.MouseMoveIntoFormClientEvnetHandler)Delegate.Combine(this.OnMouseMoveIntoFormClientEvent, new WndMsgManager.MouseMoveIntoFormClientEvnetHandler(this.OnMouseMoveIntoFormClientEvnetHandler));
			this.m_UserForm = userForm;
		}

		public new void Dispose()
		{
			base.Dispose();
			XLineJig.SizeFont.Dispose();
			XLineJig.TextFormat.Dispose();
			XLineJig.SizeTextBrush.Dispose();
			this.OnMouseLeaveFormClientEvent = (WndMsgManager.MouseLeaveFormClientEvnetHandler)Delegate.Remove(this.OnMouseLeaveFormClientEvent, new WndMsgManager.MouseLeaveFormClientEvnetHandler(this.OnMouseLeaveFormClientEvnetHandler));
			this.OnMouseMoveIntoFormClientEvent = (WndMsgManager.MouseMoveIntoFormClientEvnetHandler)Delegate.Remove(this.OnMouseMoveIntoFormClientEvent, new WndMsgManager.MouseMoveIntoFormClientEvnetHandler(this.OnMouseMoveIntoFormClientEvnetHandler));
		}

		private void OnMouseLeaveFormClientEvnetHandler(object sender, EventArgs e)
		{
			XLineJig xlineJig = sender as XLineJig;
		}

		private void OnMouseMoveIntoFormClientEvnetHandler(object sender, EventArgs e)
		{
			XLineJig xlineJig = sender as XLineJig;
		}

		public XYZ PickStartPoint(string strPrompt = "请确定直线起点：")
		{
			try
			{
				this.JigStage = XLineJig.EStage.eStartPt;
				this.StartPoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				this.StartPoint = null;
			}
			this.JigStage = XLineJig.EStage.eNoOper;
			return this.StartPoint;
		}

		public XYZ PickEndPoint(XYZ ptStart, string strPrompt = "请确定直线终点：")
		{
			try
			{
				this.JigStage = XLineJig.EStage.eEndPt;
				this.StartPoint = ptStart;
				this.EndPoint = base.UiDoc.Selection.PickPoint(strPrompt);
				this.EndPoint = this.GetResetPoint(this.StartPoint, this.EndPoint);
			}
			catch
			{
				bool flag = this.m_prompDlg != null && base.CmdEndType == XUiJigBase.ECmdEndType.eEnter;
				if (flag)
				{
					base.CmdEndType = XUiJigBase.ECmdEndType.eOther;
					XYZ xyz = base.Client2Revit(this.m_ptCur);
					Line line = Line.CreateUnbound(xyz, base.UiDoc.ActiveView.ViewDirection);
					xyz = line.Project(this.StartPoint).XYZPoint;
					xyz = this.GetResetPoint(this.StartPoint, xyz);
					string inputValue = this.m_prompDlg.GetInputValue();
					double num = NumbericTools.mmToFeet(Convert.ToDouble(inputValue));
					XYZ xyz2 = (xyz - this.StartPoint).Normalize();
					this.EndPoint = this.StartPoint + xyz2 * num;
				}
				else
				{
					this.EndPoint = null;
				}
			}
			this.JigStage = XLineJig.EStage.eNoOper;
			return this.EndPoint;
		}

		public XYZ GetResetPoint(XYZ ptBase, XYZ ptUser)
		{
			XYZ result = ptUser;
			bool flag = this.ResetPoint == XUiJigBase.EResetPoint.eByAngle;
			if (flag)
			{
				result = JigFuncs.GetProjectPoint(base.UiDoc, ptUser, ptBase, this.LineAngle);
			}
			else
			{
				bool flag2 = this.ResetPoint == XUiJigBase.EResetPoint.eByOrtho;
				if (flag2)
				{
					result = JigFuncs.GetOrthoPoint(base.UiDoc, ptUser, ptBase);
				}
				else
				{
					bool flag3 = this.ResetPoint == XUiJigBase.EResetPoint.eByAxis;
					if (flag3)
					{
						XYZ axisPoint = JigFuncs.GetAxisPoint(base.UiDoc, ptUser, ptBase);
						bool flag4 = axisPoint != null;
						if (flag4)
						{
							result = axisPoint;
						}
					}
				}
			}
			return result;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			bool flag = this.m_ptCur.IsEmpty || this.JigStage != XLineJig.EStage.eEndPt;
			if (!flag)
			{
				XYZ xyz = base.Client2Revit(this.m_ptCur);
				bool flag2 = this.ResetPoint == XUiJigBase.EResetPoint.eByAngle;
				if (flag2)
				{
					xyz = JigFuncs.GetProjectPoint(base.UiDoc, xyz, this.StartPoint, this.LineAngle);
					this.m_ptCur = base.Revit2Client(xyz);
				}
				else
				{
					bool flag3 = this.ResetPoint == XUiJigBase.EResetPoint.eByOrtho;
					if (flag3)
					{
						xyz = JigFuncs.GetOrthoPoint(base.UiDoc, xyz, this.StartPoint);
						this.m_ptCur = base.Revit2Client(xyz);
					}
					else
					{
						bool flag4 = this.ResetPoint == XUiJigBase.EResetPoint.eByAxis;
						if (flag4)
						{
							XYZ axisPoint = JigFuncs.GetAxisPoint(base.UiDoc, xyz, this.StartPoint);
							bool flag5 = axisPoint != null;
							if (flag5)
							{
								xyz = axisPoint;
								this.m_ptCur = base.Revit2Client(xyz);
								base.DrawAxisLine(paintEventArgs.Graphics, this.StartPoint, xyz);
							}
						}
					}
				}
				XGraphics xgraphics = new XGraphics(paintEventArgs.Graphics, base.JigPen);
                System.Drawing.Color color = xgraphics.m_pen.Color;
				xgraphics.m_pen.Color = this.DragLineColor;
				bool flag6 = this.MutilLineDist.Count < 1;
				if (flag6)
				{
					XLine2D xline2D = new XLine2D(base.Revit2Client(this.StartPoint), this.m_ptCur);
					xline2D.Draw(xgraphics);
					bool drawSize = this.DrawSize;
					if (drawSize)
					{
						double feet = this.StartPoint.DistanceTo(xyz);
						string strSizeText = string.Format("{0:F1}", NumbericTools.feetTomm(feet));
						XLineJig.DrawLineSize(base.Revit2Client(this.StartPoint), base.Revit2Client(xyz), strSizeText, xgraphics);
					}
				}
				else
				{
					this.DrawMutilLine(this.StartPoint, xyz, xgraphics);
				}
				xgraphics.m_pen.Color = color;
			}
		}

		public override List<Curve> CreateCurves()
		{
			List<Curve> list = new List<Curve>();
			try
			{
				bool flag = this.StartPoint != null && this.EndPoint != null;
				if (flag)
				{
					bool flag2 = this.MutilLineDist.Count < 1;
					if (flag2)
					{
						list.Add(Line.CreateBound(this.StartPoint, this.EndPoint));
					}
					else
					{
						XYZ xyz = this.StartPoint;
						XYZ xyz2 = this.EndPoint;
						XYZ xyz3 = (xyz2 - xyz).Normalize();
						Transform transform = Transform.CreateRotation(base.UiDoc.ActiveView.ViewDirection, -Math.PI*.5);
						XYZ xyz4 = transform.OfVector(xyz3);
						double num = 0.0;
						foreach (double num2 in this.MutilLineDist)
						{
							num += num2;
						}
						double num3 = 0.0;
						bool flag3 = this.PointAlignType == XLineJig.AlignType.eRight;
						if (flag3)
						{
							num3 = num;
						}
						else
						{
							bool flag4 = this.PointAlignType == XLineJig.AlignType.eMiddle;
							if (flag4)
							{
								num3 = num / 2.0;
							}
						}
						xyz -= xyz4 * num3;
						xyz2 -= xyz4 * num3;
						bool drawEndLine = this.DrawEndLine;
						if (drawEndLine)
						{
							XYZ xyz5 = xyz + xyz4 * num;
							XYZ xyz6 = xyz2 + xyz4 * num;
							list.Add(Line.CreateBound(xyz, xyz5));
							list.Add(Line.CreateBound(xyz2, xyz6));
						}
						list.Add(Line.CreateBound(xyz, xyz2));
						foreach (double num4 in this.MutilLineDist)
						{
							xyz += xyz4 * num4;
							xyz2 += xyz4 * num4;
							list.Add(Line.CreateBound(xyz, xyz2));
						}
					}
				}
			}
			catch
			{
			}
			return list;
		}

		public double LineAngle { get; set; }

		public XYZ StartPoint { get; set; }

		public XYZ EndPoint { get; set; }

		public XLineJig.EStage JigStage { get; set; }

		public XUiJigBase.EResetPoint ResetPoint { get; set; }

        public System.Drawing.Color DragLineColor { get; set; }

		public XLineJig.AlignType PointAlignType { get; set; }

		public List<double> MutilLineDist { get; set; }

		public void DrawMutilLine(XYZ ptBaseS, XYZ ptBaseE, XGraphics graphics)
		{
			Transform transform = Transform.CreateRotation(base.UiDoc.ActiveView.ViewDirection, -Math.PI*.5);
			XYZ xyz = (ptBaseE - ptBaseS).Normalize();
			XYZ xyz2 = transform.OfVector(xyz);
			double num = 0.0;
			foreach (double num2 in this.MutilLineDist)
			{
				num += num2;
			}
			double num3 = 0.0;
			bool flag = this.PointAlignType == XLineJig.AlignType.eRight;
			if (flag)
			{
				num3 = num;
			}
			else
			{
				bool flag2 = this.PointAlignType == XLineJig.AlignType.eMiddle;
				if (flag2)
				{
					num3 = num / 2.0;
				}
			}
			ptBaseS -= xyz2 * num3;
			ptBaseE -= xyz2 * num3;
			bool drawEndLine = this.DrawEndLine;
			if (drawEndLine)
			{
				XYZ point = ptBaseS + xyz2 * num;
				XYZ point2 = ptBaseE + xyz2 * num;
				XLine2D xline2D = new XLine2D(base.Revit2Client(ptBaseS), base.Revit2Client(point));
				XLine2D xline2D2 = new XLine2D(base.Revit2Client(ptBaseE), base.Revit2Client(point2));
				xline2D.Draw(graphics);
				xline2D2.Draw(graphics);
			}
			XLine2D xline2D3 = new XLine2D(base.Revit2Client(ptBaseS), base.Revit2Client(ptBaseE));
			xline2D3.Draw(graphics);
			foreach (double num4 in this.MutilLineDist)
			{
				ptBaseS += xyz2 * num4;
				ptBaseE += xyz2 * num4;
				xline2D3 = new XLine2D(base.Revit2Client(ptBaseS), base.Revit2Client(ptBaseE));
				xline2D3.Draw(graphics);
			}
		}

		public bool DrawEndLine { get; set; }

		public bool DrawSize { get; set; }

		public static int SizeOffset { get; set; }

		public static int BorderLength { get; set; }

		public static Font SizeFont { get; set; }

		public static StringFormat TextFormat { get; set; }

		public static SolidBrush SizeTextBrush { get; set; }

        public static System.Drawing.Color SizeLineColor { get; set; }

        public static void DrawLineSize(System.Drawing.Point ptStart, System.Drawing.Point ptEnd, string strSizeText, XGraphics graphics)
		{
			XUiJigBase.SortTwoPoint(ref ptStart, ref ptEnd);
			bool flag = XUiJigBase.TwoPointDist(ptStart, ptEnd) < 10;
			if (!flag)
			{
				double num = XUiJigBase.TwoPointAngle(ptStart, ptEnd);
				double dAngle = num + Math.PI*.5;
                System.Drawing.Color color = graphics.m_pen.Color;
				graphics.m_pen.Color = XLineJig.SizeLineColor;
                System.Drawing.Point point = XUiJigBase.OffsetPoint(ptStart, dAngle, XLineJig.SizeOffset);
                System.Drawing.Point point2 = XUiJigBase.OffsetPoint(ptEnd, dAngle, XLineJig.SizeOffset);
                System.Drawing.Point ptEnd2 = XUiJigBase.OffsetPoint(point, dAngle, XLineJig.BorderLength);
				XLine2D xline2D = new XLine2D(point, ptEnd2);
				xline2D.Draw(graphics);
                System.Drawing.Point ptEnd3 = XUiJigBase.OffsetPoint(point2, dAngle, XLineJig.BorderLength);
				XLine2D xline2D2 = new XLine2D(point2, ptEnd3);
				xline2D2.Draw(graphics);
                System.Drawing.Point ptFrom = XUiJigBase.OffsetPoint(point, dAngle, XLineJig.BorderLength - XLineJig.SizeOffset);
                System.Drawing.Point ptFrom2 = XUiJigBase.OffsetPoint(point2, dAngle, XLineJig.BorderLength - XLineJig.SizeOffset);
				XLine2D xline2D3 = new XLine2D(XUiJigBase.OffsetPoint(ptFrom, num, -XLineJig.SizeOffset), XUiJigBase.OffsetPoint(ptFrom2, num, XLineJig.SizeOffset));
				xline2D3.Draw(graphics);
				XLine2D xline2D4 = new XLine2D(XUiJigBase.OffsetPoint(ptFrom, num + 45.0, XLineJig.SizeOffset), XUiJigBase.OffsetPoint(ptFrom, num + 45.0, -XLineJig.SizeOffset));
				xline2D4.Draw(graphics);
				XLine2D xline2D5 = new XLine2D(XUiJigBase.OffsetPoint(ptFrom2, num + 45.0, XLineJig.SizeOffset), XUiJigBase.OffsetPoint(ptFrom2, num + 45.0, -XLineJig.SizeOffset));
				xline2D5.Draw(graphics);
                System.Drawing.Point ptFrom3 = XUiJigBase.OffsetPoint(point, num, XUiJigBase.TwoPointDist(ptStart, ptEnd) / 2);
                System.Drawing.Point point3 = XUiJigBase.OffsetPoint(ptFrom3, dAngle, (int)((double)XLineJig.BorderLength + (double)XLineJig.SizeFont.Height / 2.0));
				PointF point4 = new PointF((float)point3.X, (float)point3.Y);
				float angle = -(float)(num / Math.PI * 180.0);
				graphics.m_graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
				Matrix transform = graphics.m_graphics.Transform;
				Matrix transform2 = graphics.m_graphics.Transform;
				transform2.RotateAt(angle, point4);
				graphics.m_graphics.Transform = transform2;
				graphics.m_graphics.DrawString(strSizeText, XLineJig.SizeFont, XLineJig.SizeTextBrush, point4, XLineJig.TextFormat);
				graphics.m_graphics.Transform = transform;
				graphics.m_pen.Color = color;
			}
		}

		public enum EStage
		{
			eNoOper,
			eStartPt,
			eEndPt
		}

		public enum AlignType
		{
			eMiddle,
			eLeft,
			eRight
		}
	}
}
